﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace supermarket.method
{
    internal class Classintegralexchange
    {
        SqlConnection con = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataReader qlddr = null;  //数据缓冲区

        #region///读取礼品信息到数据表中,查询
        public void selectintegral(DataGridView dv, string uname, string unumber)
        {
            string s;
            int i = 0;
            //1.连接数据库
            Classgetsql sqlcon = new Classgetsql();
            con = sqlcon.getcon();  //数据库已连接并打开
            if (uname == "" && unumber == "")
            {
                s = "select * from Table_gift";   //无条件查询
            }
            else if (uname != "" && unumber == "")
            {
                s = "select * from Table_gift where integral < (select integral from Table_user where  name='" + uname + "')";   //根据用户名查询
            }
            else
            {
                s = "select * from Table_gift where integral < (select integral from Table_user where  number='" + unumber + "')";    //根据用户id查询
            }

            cmd = new SqlCommand(s, con);   //执行SQL语句
            qlddr = cmd.ExecuteReader();   //把sql语句执行结果读取到数据缓冲区qlddr中
            while (qlddr.Read())
            {
                dv[0, i].Value = qlddr[0].ToString();
                dv[1, i].Value = qlddr[1].ToString();
                dv[2, i].Value = qlddr[2].ToString();
                dv[3, i].Value = qlddr[3].ToString();
                i++;
            }
            qlddr.Close();
        }

        #endregion

        #region///积分兑换
        public int integralexchange(string sn,string inte, string num,string name,string number)
        {
            int i = 0;
            try
            {
                
                Classgetsql sqlcon = new Classgetsql();
                con = sqlcon.getcon();  //数据库已连接并打开
                                        //2.执行SQL语句（select）
                string s1;
                string s = "update Table_gift set number=number-'" + num + "' where giftname ='" + sn + "'";
                cmd = new SqlCommand(s, con);
                i = cmd.ExecuteNonQuery();      //受影响的行数

                if (name != "" && number == "")
                {
                   s1 = "update Table_user set integral=integral-'" + inte + "' where name='" + name + "'";
                }
                else
                {
                   s1 = "update Table_user set integral=integral-'" + inte + "' where number='" + number + "'";
                }
                cmd = new SqlCommand(s1, con);
                i = cmd.ExecuteNonQuery();
                return i;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return i = -2;
            }
        }
        #endregion
    }
}
